import { EnumUserType } from '../../utils/constant.js'
import drawQrcode from '../../utils/weapp.qrcode.esm.js';
import util from '../../utils/util';
Page({

  /**
   * 页面的初始数据
   */
  data: {

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.refreshQR()
  },

  refreshQR(){
    this.qrInterval && clearInterval(this.qrInterval)
    this.drawQR()
    this.qrInterval = setInterval(() => {
      this.drawQR()
    }, 1000 * 60)
  },
  drawQR(){
    let userType = wx.getStorageSync("userType")
    let userInfo = {}
    if(userType === EnumUserType.QiYe){
      userInfo = wx.getStorageSync("userInfo")
    } else {
      userInfo = wx.getStorageSync('user_massage')
    }
    if(!userInfo.identifyCode){
      wx.showToast({
        title: '请完善信息后，生成专属登录二维码',
        icon: 'none',
      })
      return
    }
    let curTime = new Date().getTime()
    let idCodeArr = userInfo.identifyCode.split('')
    let result = []
    for(let i = 0; i < idCodeArr.length; i+=3){
      result.push(idCodeArr.slice(i,i+3).join(''));
    }
    // 乱序
    let num = [5,1,3,0,2,4] // 正序[3,1,4,2,5,0]
    
    let chaos = [];
    result.forEach((item, index) => {
      chaos.push(result[num[index]])
    })
    let qrData = [chaos.join(''), userInfo.password, curTime, 'M']
    drawQrcode({
      width: util.rpx2px(500),
      height: util.rpx2px(500),
      canvasId: 'myQrcode',
      text: qrData.join("|"),
      // image: {
      //   imageResource: '../../images/icon.png',
      //   dx: 70,
      //   dy: 70,
      //   dWidth: 60,
      //   dHeight: 60
      // }
    })
  }
})